package com.flymobi.service;

import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.fw.modules.SM;
import com.flymobi.fw.modules.cache.CacheNeedsRefreshException;
import com.flymobi.po.Sp;
import com.flymobi.util.QL;

public class SpService extends CommonService {
	public List<Sp> list(int startRows, int rows) {
		return dao.find(Sp.class, QL.HQL.Sp.LIST,
				new Object[] {}, startRows, rows);
	}
	
	public Sp getFromCache(String spNumber,String subSpNumber){
		Sp sp=null;
		try {
			sp=(Sp)SM.CacheOscache.getFromCache("sp", spNumber+subSpNumber, 60*60);
		} catch (CacheNeedsRefreshException e) {
			sp=dao.findSingle(Sp.class,QL.HQL.Sp.FIND_BY_SPNUMBER,new Object[]{spNumber,subSpNumber});
			SM.CacheOscache.putInCache("sp", spNumber+subSpNumber, sp);
		}
		return sp;
	}

	public Long getCount() {
		return dao.getCount(QL.HQL.Sp.GET_COUNT, new Object[] {});
	}
	
	public Sp find(String name){
		return dao.findSingle(Sp.class, QL.HQL.Sp.FIND_BY_Name,
			  new Object[] {name});
	}
	
	public Sp findByNodeId(String nodeId){
		return dao.findSingle(Sp.class, "from Sp m where m.nodeId=?",
			  new Object[] {nodeId});
	}
	
	public Sp login(String name,String pwd){
		Sp u=find(name);
		if(u==null){
			return null;
		}
		if(u.getLoginPassword().equals(pwd)){
			dao.save(u);
			return u;
		}else{
			return null;
		}
	}

	public void save(Sp users) {
		dao.save(users);
	}

	public Sp load(String id) {
		return dao.load(Sp.class, new Long(id));
	}

	public Sp get(String id) {
		return dao.get(Sp.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}
}
